import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:riverpod_t/page/home/data/model/detail_model_entity.dart';
import 'package:riverpod_t/widget/app_net_image.dart';

class NovelHeaderWidget extends StatelessWidget {
  final int? type;
  final DetailModelData? model;
  final String imageUrl;

  const NovelHeaderWidget({super.key, this.model, required this.imageUrl, this.type});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // Background image
        _buildBackgroundImage(),
        // Blur overlay
        _buildBlurOverlay(),
        // Content
        _buildContent(context),
      ],
    );
  }

  Widget _buildBackgroundImage() {
    return AppNetImage(imageUrl: imageUrl, width: 1.sw, height: 280.h, fit: BoxFit.fitWidth);
  }

  Widget _buildBlurOverlay() {
    return Positioned.fill(
      child: ClipRect(
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 40.0, sigmaY: 40.0),
          child: Opacity(opacity: 0.6, child: Container(color: Colors.black54)),
        ),
      ),
    );
  }

  Widget _buildContent(BuildContext context) {
    return Container(
      height: 280.h,
      width: 1.sw,
      padding: EdgeInsets.only(
        left: 15.w,
        right: 15.w,
        top: kToolbarHeight + ScreenUtil().statusBarHeight,
      ),
      child: Column(children: [_buildBookInfoRow(), _buildFollowButton()]),
    );
  }

  Widget _buildBookInfoRow() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [_buildBookCover(), SizedBox(width: 10.w), _buildBookDetails()],
    );
  }

  Widget _buildBookCover() {
    return AppNetImage(
      imageUrl: imageUrl,
      width: 120.w,
      height: 140.w,
      fit: BoxFit.fitWidth,
      radius: 8.h,
    );
  }

  Widget _buildBookDetails() {
    final title = type == null ? model?.title ?? '' : '一个bloc完成局部刷新';
    final author = '${model?.author.name ?? ''} 著';
    const statusText = '连载中·8.28更新至第155章|34万字';

    return Flexible(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [_buildTitleText(title), _buildAuthorText(author), _buildStatusText(statusText)],
      ),
    );
  }

  Widget _buildTitleText(String title) {
    return Text(
      title,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(color: Colors.white, fontSize: 20.sp),
    );
  }

  Widget _buildAuthorText(String author) {
    return SizedBox(
      height: 40.h,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          author,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: Colors.white, fontSize: 12.sp),
        ),
      ),
    );
  }

  Widget _buildStatusText(String status) {
    return SizedBox(
      height: 40.h,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          status,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: Colors.white38, fontSize: 12.sp),
        ),
      ),
    );
  }

  Widget _buildFollowButton() {
    return Align(
      alignment: Alignment.centerRight,
      child: Container(
        margin: EdgeInsets.only(left: (1.sw + 160.w) / 2),
        width: 80.w,
        height: 30.h,
        decoration: BoxDecoration(
          color: Colors.orange,
          border: Border.all(width: 1.w, color: Colors.orange),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(6.h),
            bottomRight: Radius.circular(6.h),
          ),
        ),
        child: Center(
          child: Text(
            '追书',
            style: TextStyle(color: Colors.white, fontSize: 15.sp, fontWeight: FontWeight.w500),
          ),
        ),
      ),
    );
  }
}
